package com.situ.day3;

import org.junit.Test;

import java.util.Scanner;

public class HomeWork {

//    1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
//    求出上面数组中0-9分别出现的次数
//            双重for循环
    @Test
    public void test1() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        for (int i = 0; i < scores.length; i++) {
            switch (scores[i]) {
                case 0:
                    counts[0]++;
                    break;
                case 1:
                    counts[1]++;
                    break;
                case 2:
                    counts[2]++;
                    break;
            }
        }

        for (int i = 0; i < counts.length; i++) {
            System.out.println(counts[i]);
        }
    }

    public void test2() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        // {,,};
        for (int i = 0; i < scores.length; i++) {
           counts[scores[i]]++;
        }

        for (int i = 0; i < counts.length; i++) {
            System.out.println(counts[i]);
        }
    }

    @Test
    public void test33() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        // foreach
        for (int score : scores) {
            System.out.println(score);
        }
    }

//    题目：一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。
//    char[] array = {'a' , 'b', 'c', 'b' , 'a'};
    @Test
    public void test78() {
        char[] array = {'a' , 'b', 'c',  'b' , 'a'};
        // 初始认为是回文
        boolean isHuiWen = true;
        for (int i = 0; i < array.length / 2; i++) {
            if (array[i] != array[array.length - i -1]) {
                // 只要找到一个不符合回文的要求，就可以判断不是回文，后面也不需要判断了
                isHuiWen = false;
                break;
            }
        }

        if (isHuiWen) {
            System.out.println("是回文");
        } else {
            System.out.println("不是回文");
        }
        // 三目运算符 如果boolean类型的值是true，就执行代码1，else 执行代码2
        // boolean ? 代码1 : 代码2;
        // isHuiWen ? System.out.println("是回文") : System.out.println("不是回文");
        System.out.println(isHuiWen ? "是回文数" : "不是回文数");
    }

    // 使用循环打印100-200之间所有的素数（只能被1和自己整除的数叫素数）
    @Test
    public void test77() {
        for (int i = 100; i <= 200; i++) {
            boolean isSuShu = true;
            // i=100 ：100是不是素数
            for (int j = 2; j <= i - 1; j++) {
                if (i % j == 0) {
                    isSuShu = false;
                    break;
                }
            }

            if (isSuShu) {
                System.out.println(i + "是素数");
            } /*else {
                System.out.println(i + "不是素数");
            }*/
        }
    }

//    17、输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。
    @Test
    public void test99() {
        // "abC34 3&*%* ^&12n-"
        // {'a', 'b', 'C', '3', '4', ' ', '3'}
        // String str = "abC34 3&*%* ^&12n-";
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要统计的字符串：");
        String str = scanner.nextLine();
        System.out.println(str);
        char[] array = str.toCharArray();
        int countLetter = 0;
        int countSpace = 0;
        int countNum = 0;
        int countOther = 0;
        for (int i = 0; i < array.length; i++) {
            // () || ()
            if ((array[i] >= 'a' && array[i] <= 'z')
                    || (array[i] >= 'A' && array[i] <= 'Z')) {
                countLetter++;
            } else if (array[i] == ' ') {
                countSpace++;
            } else if (array[i] >= '0' && array[i] <= '9') {
                countNum++;
            } else {
                countOther++;
            }
        }

        System.out.println("字母的个数: " + countLetter);
        System.out.println("空格的个数: " + countSpace);
        System.out.println("数字的个数: " + countNum);
        System.out.println("其他的个数: " + countOther);
    }

}
